/**
 * Created by Administrator on 2016/12/2.
 */
class PublicVal {

    static i: PublicVal = new PublicVal;

    static state: number;

    //庄家
    zhuangFlag: number;
    //房主
    roomOwnFlag: number;
    //宝牌
    bao: any;
    //庄家pos
    zhuang: number;
    //回放版本号
    replayVer: string;

    allPais: any = [];

    ownPos: number;

    dirPerson: any;
    posPerson: any;

    //断线重连后是否听牌
    backTing: number;
    dir2Pos: any = {};
    pos2Dir: any = {};

    constructor() {
        this.clear();
    }

    clear() {
        this.bao = null;
        this.zhuangFlag = 0;

        //handPaiBaseLen 手牌长度基值(粘听牌|支对)
        this.allPais[1] = { handPais: null, catchPai: null, funcPais: [], poolPais: [], handPaiBaseLen: 0 };
        this.allPais[2] = { handPais: null, catchPai: null, funcPais: [], poolPais: [], handPaiBaseLen: 0 };
        this.allPais[3] = { handPais: null, catchPai: null, funcPais: [], poolPais: [], handPaiBaseLen: 0 };
        this.allPais[4] = { handPais: null, catchPai: null, funcPais: [], poolPais: [], handPaiBaseLen: 0 };
    }

    //获取手牌
    getHandPais(dir: number) {
        return this.allPais[dir].handPais;
    }

    //添加手牌
    addHandPai(dir: number, pai: any, sort: boolean = false) {
        if (!this.getPos(dir)) return;

        let pais: any[] = this.getHandPais(dir);
        pais.push(pai);

        if (sort) FashionTools.sortPai(pais);
    }

    //移除手牌
    removeHandPai(dir: number, pai: any = null, sort: boolean = true) {
        let pais = this.getHandPais(dir);
        if (pai == null || pai == undefined) {
            pais.length--;
            return;
        }
        FashionTools.removePai(pais, pai);
        if (sort) FashionTools.sortPai(pais);
    }

    //获取池牌
    getPoolPais(dir: number) {
        return this.allPais[dir] ? this.allPais[dir].poolPais : null;
    }

    //池牌添加
    pushPoolPai(dir: number, pai: any) {
        let poolPais = PublicVal.i.getPoolPais(dir);
        if (poolPais) {
            pai.poolIndex = poolPais.length;
            poolPais.push(pai);
        }
    }

    //池牌取出
    popPoolPai(dir: number) {
        let poolPais = PublicVal.i.getPoolPais(dir);
        if (poolPais) {
            poolPais.length && poolPais.length--;
        }
    }

    //获取功能牌
    getFuncPais(dir: number) {
        return this.allPais[dir].funcPais;
    }

    //往功能牌型里添加牌 排序 方位 功能 牌
    addFuncPai(sort: number, dir: number, action: number, pai: any | any[], number: number = 0, ever: any = null) {
        let funcPais = PublicVal.i.getFuncPais(dir);
        if (ever != null) ever = [1, 1, 1];
        if (action == 1) {
            pai[0].number > pai[2].number && pai.reverse();
        }

        let addPaiObj: any = { pai: pai, number: number, ever: ever };
        for (let i: number = 0; i < funcPais.length; i++) {
            let obj = funcPais[i];
            if (obj.action == action) {
                obj.pais.push(addPaiObj);
                return;
            }
        }
        funcPais.push({ sort: sort, action: action, pais: [addPaiObj] });
    }

    //删除已经碰的功能牌
    removePengFunc(dir: number, pai: any): boolean {
        let funcPais = PublicVal.i.getFuncPais(dir);
        for (let i: number = 0; i < funcPais.length; i++) {
            let obj = funcPais[i];
            if (obj.action == 2) {
                let pengObjs = obj.pais;
                for (let k: number = 0; k < pengObjs.length; k++) {
                    let kObj = pengObjs[k];
                    if (kObj.pai[0].type == pai.type && kObj.pai[0].number == pai.number) {
                        pengObjs.splice(k, 1);
                        return true;
                    }
                }
            }
        }
        return false;
    }

    //获取含有ever的对象
    getPai(dir: number, action: number, number: number = 0) {
        let funcPais = PublicVal.i.getFuncPais(dir);
        for (let i: number = 0; i < funcPais.length; i++) {
            let funcPai = funcPais[i];
            if (funcPai.action == action) {
                for (let j: number = 0; j < funcPai.pais.length; j++) {
                    let obj = funcPai.pais[j];
                    //幺九杠
                    if (number > 0) {
                        if (number == obj.number) {
                            return obj;
                        }
                    } else {
                        return obj;
                    }
                }
            }
        }
        return null;
    }

    //设置手牌基础长度
    setHandPaiBaseLen(dir: number, len: number) {
        this.allPais[dir].handPaiBaseLen = len;
    }

    //获取手牌基础长度
    getHandPaiBaseLen(dir: number) {
        return this.allPais[dir].handPaiBaseLen;
    }

    //通过pos取得方位
    getDir(pos: number) {
        return this.pos2Dir[pos];
    }

    //通过方位获取pos
    getPos(dir: number) {
        return this.dir2Pos[dir];
    }

    //----------------------------模拟牌型显示

    //模拟吃牌
    moniChiPai(pai: any) {
        let fromDir: number = PublicVal.i.getDir(pai[1].pos);

        this.addFuncPai(5, 1, 1, pai);
        this.removeHandPai(1, pai[0], false);
        this.removeHandPai(1, pai[2], true);

        if (fromDir > 0) {
            PublicVal.i.popPoolPai(fromDir);
            GSController.i.removePoolCard(fromDir);
        }

        GameSound.play("sound_down");
        GSController.i.updateMJView(1);
        gameManager.i.dispatchEvent(EffectEvent.CardThrowTips);

    }

    //模拟碰牌
    moniPengPai(pai: any) {
        let fromDir: number = PublicVal.i.getDir(pai[1].pos);

        this.addFuncPai(4, 1, 2, pai);
        this.removeHandPai(1, pai[0], false);
        this.removeHandPai(1, pai[2], true);

        if (fromDir > 0) {
            PublicVal.i.popPoolPai(fromDir);
            GSController.i.removePoolCard(fromDir);
        }

        GameSound.play("sound_down");
        GSController.i.updateMJView(1);
        gameManager.i.dispatchEvent(EffectEvent.CardThrowTips);
    }

    //模拟粘牌
    moniZhanPai(pai: any) {
        let fromDir: number = PublicVal.i.getDir(pai[1].pos);

        this.addFuncPai(6, 1, 224, pai);
        this.removeHandPai(1, pai[0], true);

        if (fromDir > 0) {
            PublicVal.i.popPoolPai(fromDir);
            GSController.i.removePoolCard(fromDir);
        }

        GameSound.play("sound_down");
        GSController.i.updateMJView(1);
        gameManager.i.dispatchEvent(EffectEvent.CardThrowTips);
    }
}